package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.DruDrug;
import com.ruoyi.project.drug.domain.DruManufacturer;
import com.ruoyi.project.drug.service.DruManufacturerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@Transactional
@RequestMapping("/drug/manufacturer")
public class DruManufacturerController extends BaseController {

    @Autowired
    private DruManufacturerService druManufacturerService;



    /**
     * 无条件查询所有生产工厂信息
     *
     * @return 返回所有数据集合
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:manufacturer:selectAll')")
    @GetMapping("/selectManufacturer")
    public TableDataInfo selectManufacturer(){

        List<DruManufacturer> list = druManufacturerService.selectManufacturer();

        return getDataTable(list);
    }


    /**
     *
     * 根据条件进行查询，在使用对数据进行分页显示
     *
     * @param druManufacturer  条件查询参数
     * @return  返回查询结果集合
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:manufacturer:selectAll')")
    @GetMapping("/selectAll")
    public TableDataInfo selectAll(DruManufacturer druManufacturer){

        startPage();

        List<DruManufacturer> list = druManufacturerService.selectAll(druManufacturer);

        return getDataTable(list);
    }




    /**
     * 添加生产厂家
     *
     * @param druManufacturer  用来传递生产厂家的参数数据
     * @return  返回 1 成功  否则失败
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:manufacturer:addManufacturer')")
    @PostMapping("/addManufacturer")
    public AjaxResult addManufacturer(@RequestBody DruManufacturer druManufacturer) {

        druManufacturer.setCreateBy(SecurityUtils.getUsername());
        druManufacturer.setCreateTime(new Date());

        int i = druManufacturerService.addManufacturer(druManufacturer);

        return toAjax(i);
    }



    /**
     * 根据id查询单个生产厂家信息进行数据回显
     *
     * @param dmid  生产厂家主键id
     * @return  返回查询结果
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:manufacturer:updateManufacturer')")
    @GetMapping("/selectOne")
    public AjaxResult selectOne(int dmid) {
        DruManufacturer druManufacturer = druManufacturerService.selectOne(dmid);
        return AjaxResult.success(druManufacturer);
    }



    /**
     * 修改生产厂家信息
     *
     * @param druManufacturer  用来传递生产厂家的参数数据
     * @return  返回 1 修改成功  否则失败
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:manufacturer:updateManufacturer')")
    @PutMapping("/updateManufacturer")
    public AjaxResult updateManufacturer(@RequestBody DruManufacturer druManufacturer) {

        druManufacturer.setUpdateBy(SecurityUtils.getUsername());
        druManufacturer.setUpdateTime(new Date());

        int i = druManufacturerService.updateManufacturer(druManufacturer);
        return toAjax(i);
    }




    /**
     * 根据主键删除单条或多条生产厂家信息
     *
     * @param dmids  生产厂家主键id数组
     * @return  返回 1 删除成功  否则失败
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:manufacturer:deleteManufacturer')")
    @DeleteMapping("/deleteManufacturer")
    public AjaxResult deleteManufacturer(int[] dmids) {

        int i = druManufacturerService.deleteManufacturer(dmids);
        return toAjax(i);
    }
}
